package com.zzw.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.buaa.activity.MainActivity;
import com.buaa.activity.QuestionActivity;
import com.buaa.activity.R;
import com.buaa.activity.SearchActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/4/15.
 */
public class BaseActivity extends AppCompatActivity {
    private TextView topTitle;

    private Class[] targetActivities = new Class[]{MainActivity.class, QuestionActivity.class, SearchActivity.class};

    private Button[] bottomBtns = new Button[3];

    //没有被选中时按钮的图片
    private int[] bottomImgs = new int[]{R.drawable.bottombtn01b, R.drawable.bottombtn02b, R.drawable.bottombtn03b};

    private int[] bottomImgsSelected = new int[]{R.drawable.bottombtn01a, R.drawable.bottombtn02a, R.drawable.bottombtn03a};

    private int i;

    private Button setting;
    private PopupWindow win;

    private static List<Activity> allActivities = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        allActivities.add(this);
        super.onCreate(savedInstanceState);

    }

    protected void init(String title, int index) {
        topTitle = (TextView) findViewById(R.id.top_title);
        topTitle.setText(title);

        bottomBtns[0] = (Button) findViewById(R.id.bottom_btn_01);
        bottomBtns[1] = (Button) findViewById(R.id.bottom_btn_02);
        bottomBtns[2] = (Button) findViewById(R.id.bottom_btn_03);

        for(i=0;i<bottomBtns.length;i++) {
            final int temp = i;
            if(i==index) {
                bottomBtns[i].setBackgroundResource(bottomImgsSelected[i]);
            }else {
                bottomBtns[i].setBackgroundResource(bottomImgs[i]);
                bottomBtns[i].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent in = new Intent(BaseActivity.this, targetActivities[temp]);
                        startActivity(in);
                    }
                });
            }
        }

        setting = (Button) findViewById(R.id.setting);
        setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                win = new PopupWindow(Globals.SCREEN_WIDTH/4, Globals.SCREEN_HEIGHT/6);
                View view = LayoutInflater.from(BaseActivity.this).inflate(R.layout.win_list,null);
                win.setContentView(view);

                if(win.isShowing()) {
                    win.dismiss();
                }else {
                    //把浮动窗口置于设置按钮下方
                    win.showAsDropDown(setting);
                }

                TextView version = (TextView)view.findViewById(R.id.win_version);
                TextView about = (TextView)view.findViewById(R.id.win_about);
                TextView exit = (TextView)view.findViewById(R.id.win_exit);

                version.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        win.dismiss();
                        Toast.makeText(BaseActivity.this,"当前为1.0.1版本",Toast.LENGTH_LONG).show();
                    }
                });

                about.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        win.dismiss();
                        AlertDialog.Builder builder = new AlertDialog.Builder(BaseActivity.this);
                        builder.setTitle("提示信息");
                        builder.setMessage("此应用为北航团队制作");
                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }

                        });
                        builder.create().show();
                    }
                });

                exit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        win.dismiss();
                        for(Activity a : allActivities) {
                            a.finish();
                        }
                    }
                });
            }
        });
    }

    @Override
    protected void onDestroy() {
        allActivities.remove(this);
        super.onDestroy();
    }
}
